package com.rzl.expense.quartz.task;


import com.rzl.expense.agent.DataAgentService;
import com.rzl.expense.common.utils.DateUtils;
import com.rzl.expense.expense.domain.DatBasetower;
import com.rzl.expense.expense.domain.RptPrvEleSitePayable;
import com.rzl.expense.expense.domain.RptPrvTowerKeyIndexCope;
import com.rzl.expense.expense.service.IRptPrvEleSitePayableService;
import com.rzl.expense.expense.service.IRptPrvTowerKeyIndexCopeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 电费应付月报
 *
 * @author admin
 * @date 2023/08/09
 */
@Slf4j
@Component("RptPrvTowerKeyIndexCopeTask")
public class RptPrvTowerKeyIndexCopeTask {
    @Autowired
    private DataAgentService dataAgentService;

    @Autowired
    private IRptPrvTowerKeyIndexCopeService rptPrvTowerKeyIndexCopeService;


    public void run() {
        log.info("获取集团用户数据..........");
        // 执行sql
            int size;
            // limit起始位置
            int startIndex = 0;
            // limit大小
            int limit = 20000;
        rptPrvTowerKeyIndexCopeService.deleteRptPrvTowerKeyIndexCope();
            do {

                List<RptPrvTowerKeyIndexCope> list = (List<RptPrvTowerKeyIndexCope>) dataAgentService.getData(RuleSqlAdapter.getRptPrvTowerKeyIndexCope(startIndex, limit), RptPrvTowerKeyIndexCope.class);
                log.info("获取集团用户数据：{}条", list.size());
                if (!Objects.isNull(list) && list.size() > 0) {
                    int insertSize = list.size();
                    // limit起始位置
                    int insertIndex = 0;
                    do {
                        List<RptPrvTowerKeyIndexCope> rptPrvEleSitePayables = list.stream().skip(insertIndex).limit(2000).collect(Collectors.toList());
                        try {
                            if (!Objects.isNull(rptPrvEleSitePayables) && rptPrvEleSitePayables.size() > 0) {
                                rptPrvTowerKeyIndexCopeService.batchRptPrvTowerKeyIndexCope(rptPrvEleSitePayables);
                                insertSize = rptPrvEleSitePayables.size();
                                insertIndex = insertIndex + 2000;
                            } else {
                                insertSize = 0;
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } while (insertSize >= 2000);
                }
                size = list.size();
                startIndex = startIndex + limit;
            } while (size >= limit);

        }

}
